﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Mvc.XForms.UI {
	public class DefaultPagerFormat : IPagerFormatter {
		private const string first = "First";
		private const string last = "Last";
		private const string previous = "Previous";
		private const string next = "Next";

		public virtual string First {
			get { return first; }
		}
		public virtual string Last {
			get { return last; }
		}
		public virtual string Previous {
			get { return previous; }
		}
		public virtual string Next {
			get { return next; }
		}

		public virtual string Pager(IPaged pagedObj, string pagedUrlTemplate) {
			if (pagedObj == null)
				throw new InvalidOperationException("Grid model does not inherit from IPaged");

			var firstText = GetFirstLink(pagedObj, pagedUrlTemplate);
			var previousText = GetPreviousLink(pagedObj, pagedUrlTemplate);
			var pageListText = GetPageLinks(pagedObj, pagedUrlTemplate);
			var nextText = GetNextLink(pagedObj, pagedUrlTemplate);
			var lastText = GetLastLink(pagedObj, pagedUrlTemplate);

			var paging = new StringBuilder("<div class=\"paging\">");
			paging.Append(firstText);
			paging.Append(previousText);
			paging.Append(pageListText);
			paging.Append(nextText);
			paging.Append(lastText);
			paging.Append("</div>");

			return paging.ToString();
		}

		public virtual string GetFirstLink(IPaged pagedObj, string pagedUrlTemplate) {
			if (!pagedObj.IsPrevious)
				return "<span class=\"first disabled\">&#171; " + First + "</span>";

			return string.Format("<a href=\"" + pagedUrlTemplate + "\" class=\"first\">&#171; {1}</a>", 1, First);
		}

		public virtual string GetLastLink(IPaged pagedObj, string pagedUrlTemplate) {
			if (!pagedObj.IsNext)
				return "<span class=\"last disabled\">" + Last + " &#187;</span>";

			return string.Format("<a href=\"" + pagedUrlTemplate + "\" class=\"last\">{1} &#187;</a>", pagedObj.PageCount, Last);
		}

		public virtual string GetPreviousLink(IPaged pagedObj, string pagedUrlTemplate) {
			if (!pagedObj.IsPrevious)
				return "<span class=\"disabled\">&#171; " + Previous + "</span>";

			var page = pagedObj.PageIndex == 1 ? 1 : pagedObj.PageIndex - 1;

			return string.Format("<a href=\"" + pagedUrlTemplate + "\">&#171; {1}</a>", page, Previous);
		}

		public virtual string GetNextLink(IPaged pagedObj, string pagedUrlTemplate) {
			if (!pagedObj.IsNext)
				return "<span class=\"disabled\">" + Next + " &#187;</span>";

			var page = pagedObj.PageIndex < pagedObj.PageCount ? pagedObj.PageIndex + 1 : pagedObj.PageCount;

			return string.Format("<a href=\"" + pagedUrlTemplate + "\">{1} &#187;</a>", page, Next);
		}

		public virtual string GetPageLinks(IPaged pagedObj, string pagedUrlTemplate) {
			var pageListText = new StringBuilder();

			var listFirst = pagedObj.PageIndex - 2;
			if (pagedObj.PageIndex < 5)
				listFirst = 1;
			else if (pagedObj.PageCount - pagedObj.PageIndex < 2)
				listFirst = pagedObj.PageCount - 4;

			var listEnd = listFirst + 4;
			if (listEnd > pagedObj.PageCount)
				listEnd = pagedObj.PageCount;

			for (var i = listFirst; i <= listEnd; i++) {
				var listItem = string.Format("<a href=\"{0}\">{{0}}</a>", pagedUrlTemplate);
				if (i != pagedObj.PageIndex)
					pageListText.AppendFormat(listItem, i);
				else
					pageListText.AppendFormat("<span>{0}</span>", i);
			}

			return pageListText.ToString();
		}
	}
}
